// pages/payment/postBar/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    package_id: '',
    flag_recharge: true,
  },
  go_recharge() {
    this.setData({
      flag_recharge: true,
    })
    wx.navigateTo({
      url: '../bodou/index',
    })
  },
  modal_hide() {
    this.setData({
      flag_recharge: true,
    })
  },
  recharge() {
    wx.navigateTo({
      url: '../bodou/index'
    })
  },
  clickPay() {
    let recruit_id = this.data.recruit_id,
      package_id = this.data.package_id,
      usable_bd = this.data.usable_bd,
      hf_bd = this.data.hf_bd,
      is_already_full = this.data.is_already_full;
    if (package_id == '') {
      wx.showToast({
        title: '请选择套餐',
        icon: 'none'
      })
    } else {
      if (is_already_full == 0) {
        //职位未满
        console.log('hf_bd+' + hf_bd)
        console.log('usable_bd+' + usable_bd)
        if (parseInt(usable_bd) >= parseInt(hf_bd)) {
          //波豆余额足够
          this.pay(recruit_id, package_id)
        } else {
          // console.log('提示充值')
          this.setData({
            flag_recharge: false,
          })
        }
      } else {
        wx.showToast({
          title: '您好,本职位栏服务已售空,请稍后进行购买。',
          icon: 'none'
        })
      }
    }

  },
  pay(recruit_id, package_id) {
    wx.request({
      url: app.globalData.url + 'api/recruit/upgrade',
      data: {
        token: app.globalData.token,
        recruit_id: recruit_id,
        package_id: package_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e);
        // wx.showToast({
        //   title: '恭喜您,购买成功',
        //   icon: 'none'
        // })
        wx.showModal({
          content: '恭喜您,购买成功',
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#d0a76c',
          success: (e) => {
            if (e.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            } 
          }
        })
        this.getOption();
      }
    })
  },
  click_select(e) {
    // console.log(e)
    this.setData({
      package_id: '',
      usable_bd: '',
      hf_bd: '',
    })
    let package_id = e.currentTarget.dataset.package_id,
      usable_bd = e.currentTarget.dataset.usable_bd,
      index = e.currentTarget.dataset.index,
      hf_bd = e.currentTarget.dataset.hf_bd;
    this.setData({
      package_id: package_id,
      usable_bd: usable_bd,
      index: index,
      hf_bd: hf_bd,
    })
  },
  getOption() {
    wx.request({
      url: app.globalData.url + 'api/recruit/upgrade/package',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let list = e.data.data.list,
          is_already_full = e.data.data.is_already_full,
          usable_bd = e.data.data.usable_bd;
        this.setData({
          list: list,
          usable_bd: usable_bd,
          is_already_full: is_already_full,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.recruit_id)
    let recruit_id = options.recruit_id ? options.recruit_id : "";
    this.setData({
      recruit_id: recruit_id,
    })
    this.getOption();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})